package com.xtoneict.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xtoneict.auth.entity.Attendance;
import com.xtoneict.auth.entity.LeaveDay;
import com.xtoneict.auth.entity.LeaveDetail;
import com.xtoneict.auth.mapper.AttendanceMapper;
import com.xtoneict.auth.mapper.LeaveDayMapper;
import com.xtoneict.auth.mapper.LeaveDetailMapper;
import com.xtoneict.auth.service.IAttendanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hx
 * @since 2024-06-19
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements IAttendanceService {
    @Autowired
    private LeaveDetailMapper leaveDetailMapper;
    @Autowired
    private LeaveDayMapper leaveDayMapper;

    /**
     * 计算某员工某月请假总天数
     * @param attendanceId
     * @return
     */
    public double leaveDayCount(Integer attendanceId){
        double leaveDaysCount = 0;

        QueryWrapper<LeaveDetail> leaveDetailQueryWrapper = new QueryWrapper<>();
        leaveDetailQueryWrapper.eq("attendance_id",attendanceId);
        List<LeaveDetail> leaveDetails = leaveDetailMapper.selectList(leaveDetailQueryWrapper);

        for (LeaveDetail leaveDetail : leaveDetails){
            QueryWrapper<LeaveDay> leaveDayQueryWrapper = new QueryWrapper<>();
            leaveDayQueryWrapper.eq("leave_detail_id", leaveDetail.getLeaveDetailId());
            List<LeaveDay> leaveDays = leaveDayMapper.selectList(leaveDayQueryWrapper);

            for (LeaveDay leaveDay : leaveDays){
                if(leaveDay.isHalfDay()){
                    leaveDaysCount += 0.5;
                }else {
                    leaveDaysCount += 1;
                }
            }
        }

        return leaveDaysCount;
    }
}
